import type { Token } from '@/types/data'
import type { LoginData, UserInfo } from '@/types/user'

type Req<T> = {
  url: string
  body: T
  query: Record<string, never>
  headers: {
    [key: string]: string
    host: string
    connection: string
    origin: string
    'sec-fetch-site': string
    'sec-fetch-mode': string
    'sec-fetch-dest': string
    referer: string
    'accept-encoding': string
    'accept-language': string
  }
}

const menus = [
  {
    name: '后台面板',
    icon: 'home-filled',
    path: '/'
  },
  {
    name: '系统管理',
    icon: 'Tools',
    child: [
      {
        name: '用户管理',
        icon: 'UserFilled',
        path: '/admin/user'
      },
      {
        name: '角色管理',
        icon: 'Avatar',
        path: '/admin/role'
      },

      {
        name: '权限管理',
        icon: 'Menu',
        path: '/admin/authority'
      }
    ]
  }
]

export default [
  // 用户登录
  {
    url: '/api/login',
    method: 'POST',
    response: (req: Req<LoginData>) => {
      const { username, password } = req.body

      if (username === 'admin' && password === 'admin123') {
        return {
          code: 20000,
          success: true,
          message: '请求成功。',
          data: <Token>{
            accessToken: '12335345241223'
          }
        }
      } else {
        return {
          code: 50000,
          success: false,
          message: '用户名或密码错误。',
          data: {}
        }
      }
    }
  },
  // 获取用户信息
  {
    url: '/api/user',
    method: 'GET',
    response: (req: any) => {
      const token = req.headers.token
      if (token === '12335345241223') {
        return {
          code: 20000,
          success: true,
          message: '请求成功。',
          data: <UserInfo>{
            id: '1',
            username: 'admin',
            avatar: '',
            menus: menus
          }
        }
      } else {
        return {
          code: 50000,
          success: false,
          message: 'token验证错误',
          data: {}
        }
      }
    }
  },
  // 注销登录
  {
    url: '/api/logout',
    method: 'POST',
    response: () => {
      return {
        code: 20000,
        success: true,
        message: '请求成功。',
        data: '注销成功'
      }
    }
  }
]
